/**
 * 2024.8.8 首次创建 by 段晓龙 修改时间：2024.8.8
 * 路由信息 （如有改动，记得更新）
 * 2024.8.8 项目初创 由Jquery迁移到Vue3 路由信息是由后端请求，前端渲染
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/layout/layoutMain.vue'
import HomeView from '@/views/home/index.vue' // 首页
import SearchView from '@/views/search/index.vue' // 搜索
// import MyTerritoryView from '@/views/myTerritory/index.vue' // 我的地盘
import LoginView from '@/views/login/index.vue' // 登录
import LoginsView from '@/views/login/logins.vue' // 单点登录
import PasswordModification from '@/views/login/passwordModification.vue' // 密码修改
import Erroe40x from '@/views/error/error40x.vue' // 404
import Error50x from '@/views/error/error50x.vue' // 505
import NProgress from 'nprogress'
import { useUserStore } from '@/stores'
import { ref } from 'vue'
import { ElLoading } from 'element-plus'

export const routes = [
  {
    path: '/',
    name: 'layoutIndex',
    redirect: '/home',
    component: Layout,
    children: [
      {
        path: '/home',
        name: 'homeIndex',
        meta: {
          title: '首页',
          keepAlive: true,
          hidden: false
        },
        component: HomeView
      },
      {
        path: '/search',
        name: 'searchIndex',
        meta: {
          title: '搜索',
          keepAlive: false,
          hidden: true
        },
        component: SearchView
      },
      // {
      //   path: '/myTerritory',
      //   name: 'myTerritoryIndex',
      //   meta: {
      //     title: '我的地盘',
      //     keepAlive: false,
      //     hidden: true
      //   },
      //   component: MyTerritoryView
      // },
      {
        path: '/passwordModification',
        name: 'passwordModification',
        meta: {
          title: '密码修改',
          keepAlive: false,
          hidden: true
        },
        component: PasswordModification
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/logins',
    name: 'logins',
    component: LoginsView
  },
  {
    path: '/error40x',
    name: '40x',
    component: Erroe40x
  },
  {
    path: '/error50x',
    name: 'Error50x',
    component: Error50x
  }
]
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes
})

const registerRouteFresh = ref(true)

let store: any = null
let loading: any = null
router.beforeEach((to, from, next) => {
  NProgress.start()
  loading = ElLoading.service({
    lock: true,
    text: '页面正在加载中，请稍候...',
    background: 'rgba(255, 255, 255, 0.7)'
  })
  // 用户仓库
  store = useUserStore() // 用户信息
  const wihteList = ['/login', '/logins', '/error40x', '/error50x']
  if (!store.user && !wihteList.includes(to.path)) return next('/login')
  if (store.user && !wihteList.includes(to.path) && registerRouteFresh.value) {
    store.creatNavBar()
    registerRouteFresh.value = false
    next({ ...to, replace: true })
  } else {
    if (to.path === '/XiTongGuanLi') {
      return next('/XiTongGuanLi/XiTongGuanLi/JueSeGuanLi')
    } else {
      next()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
  loading.close()
})

export default router
